package com.cbw.controller;

import com.cbw.bean.BootStrapTableCourier;
import com.cbw.bean.Courier;
import com.cbw.bean.Message;
import com.cbw.bean.ResultData;
import com.cbw.mvc.ResponseBody;
import com.cbw.service.CourierService;
import com.cbw.service.UserService;
import com.cbw.util.DateFormatUtil;
import com.cbw.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //接收参数 无
        //调用service
        List<Map<String, Integer>> list = CourierService.console();//list中存放了一个map,map中存放两个键值对size day_num
        //填充消息对象
        Message msg = new Message();
        if (list.size()>0){
            msg.setStatus(0);
            msg.setData(list);
        }else {
            msg.setStatus(-1);
        }
        //处理json
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //获取参数(分页:开始索引、每页记录数)
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //调用service，分页查询
        List<Courier> list = CourierService.list(true, offset, pageNumber);
        //将Courier改成BootStrapTableCourier类型，将除了int外其它类型改为string
        ArrayList<BootStrapTableCourier> list2 = new ArrayList<>();
        for (Courier courier : list) {
            String createTime = DateFormatUtil.formatToString(courier.getCreateTime());
            String lasTime=courier.getLasTime()==null?"尚未登录":DateFormatUtil.formatToString(courier.getLasTime());
            BootStrapTableCourier c2 = new BootStrapTableCourier(courier.getId(), courier.getNumber(), courier.getUsername(), courier.getPhone(), courier.getCardId(), courier.getPassword(), courier.getSendNumber(), createTime, lasTime);
            list2.add(c2);
        }
        //填充分页对象
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(CourierService.total());
        //分页时，注意这里只能将分页对象传递给前台，不能将分页对象再封装到消息对象传回，否则前台获取不到分页数据，bootstrap-table分页格式是固定的，前台只接受分页对象才能展示数据
        //json处理
        String json = JSONUtil.toJSON(data);
        return json;//传给DispatchServlet
        //测试:http://localhost:8080/courier/list.do?offset=0&pageNumber=5
    }

    @ResponseBody("/courier/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response){
        //1.接收参数
        Courier courier = new Courier();
        courier.setUsername( request.getParameter("username"));
        courier.setPhone(request.getParameter("phone"));
        courier.setCardId(request.getParameter("cardId"));
        courier.setPassword(request.getParameter("password"));
        //注册时间，在sql中通过now()获取当前时间，而由管理员新添加的快递员不存在上次登录时间，为Null,在页面显示"尚未登录"
        //2.调用service，添加快递员
        boolean insert = CourierService.insert(courier);
        //3.填充消息对象
        Message msg = new Message();
        if (insert){
            msg.setStatus(0);
            msg.setResult("添加成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("添加失败");
        }
        //处理Json
        String json = JSONUtil.toJSON(msg);
        return json; //传给DispatchServlet
        //测试:http://localhost:8080/courier/add.do?username=cbw&phone=111111&cardId=111111&password=111
    }

    @ResponseBody("/courier/find.do")
    public String findByPhone(HttpServletRequest request, HttpServletResponse response){
        //获取参数
        String phone = request.getParameter("phone");
        System.out.println("手机号:"+phone);
        //调用service,查询
        Courier courier = CourierService.findByPhone(phone);
        //填充参数
        Message msg = new Message();
        if (courier!=null){
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(courier);
        }else {
            msg.setStatus(-1);
            msg.setResult("查询失败");
        }
        //转json
        String json = JSONUtil.toJSON(msg);
        return json;
        //测试:http://localhost:8080/courier/find.do?phone=12356
    }


    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        //1.接收参数
        Courier courier = new Courier();
        courier.setId(Integer.parseInt(request.getParameter("id")));
        courier.setUsername( request.getParameter("username"));
        courier.setPhone(request.getParameter("phone"));
        courier.setCardId(request.getParameter("cardId"));
        courier.setPassword(request.getParameter("password"));
        //调用service更新
        boolean update = CourierService.update(courier);
        //填充消息对象
        Message msg = new Message();
        if (update){
            msg.setStatus(0);
            msg.setResult("更新成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("更新失败");
        }
        //转成Json
        String json = JSONUtil.toJSON(msg);
        return json;
        //测试http://localhost:8080/courier/update.do?id=43&username=123&phone=123&cardId=123&password=123
    }

    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //获取参数
        int id = Integer.parseInt(request.getParameter("id"));
        //调用service，删除
        boolean delete = CourierService.delete(id);
        //填充消息对象
        Message msg = new Message();
        if (delete){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        //转json
        String json = JSONUtil.toJSON(msg);
        return json;
        //测试http://localhost:8080/courier/delete.do?id=43
    }


}
